package com.wd.demo1.persistent;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Administrator
 * @date 2025-10-31 0:02
 */
@Service
@State(name = "WDDemoPersistent", storages = @Storage("wd.demo1.xml"))
public final class DemoPersistent implements PersistentStateComponent<DemoPersistent> {

	private String path;

	private DemoPersistent() {
	}

	public static DemoPersistent getInstance(Project project) {
		DemoPersistent demoPersistent = project.getService(DemoPersistent.class);
		if (demoPersistent == null) {
			demoPersistent = new DemoPersistent();
		}
		return demoPersistent;
	}

	public static DemoPersistent getInstance() {
		DemoPersistent persistent = ApplicationManager.getApplication().getService(DemoPersistent.class);
		if (persistent == null) {
			persistent = new DemoPersistent();
		}
		return persistent;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public @Nullable DemoPersistent getState() {
		return this;
	}

	@Override
	public void loadState(@NotNull DemoPersistent demoPersistent) {
		XmlSerializerUtil.copyBean(demoPersistent, this);
	}
}
